package com.jdjw.modules.buscomplaint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.buscomplaint.entity.BusComplaint;
import com.jdjw.modules.buscomplaint.mapper.BusComplaintMapper;
import com.jdjw.modules.buscomplaint.service.BusComplaintService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.modules.buscomplaintaccept.entity.BusComplaintAccept;
import com.jdjw.modules.buscomplaintaccept.mapper.BusComplaintAcceptMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 群众投诉表 服务实现类
 * </p>
 *
 * @author fq
 * @since 2021-06-10
 */
@Service
public class BusComplaintServiceImpl extends ServiceImpl<BusComplaintMapper, BusComplaint> implements BusComplaintService {


    @Resource
    BusComplaintMapper busComplaintMapper;
    @Resource
    BusComplaintAcceptMapper busComplaintAcceptMapper;

    @Override
    public ResponseResult addComplaintInfo(BusComplaint complaint) {
        ResponseResult result = new ResponseResult();
        try{
            save(complaint);
            result.setCode(200);
            result.setMsg("SUCCESS");
            result.setData("推送成功");
        }
        catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("ERROR");
            result.setData("请求失败");
        }
        return result;
    }

    @Override
    public ResponseResult getComplaintData(String id) {
        ResponseResult result = new ResponseResult();
        try{
            Map<Object,Object> map = new HashMap<>();
            BusComplaint busComplaint = getById(id);
            map.put("busComplaint",busComplaint);

            List<BusComplaintAccept> complaintAccepts = new ArrayList<>();
            if (busComplaint!=null){
                QueryWrapper queryAcceptWrapper = new QueryWrapper();
                queryAcceptWrapper.eq("complaint_id",busComplaint.getId());
                complaintAccepts = busComplaintAcceptMapper.selectList(queryAcceptWrapper);
            }
            map.put("complaintAccepts",complaintAccepts);

            result.setCode(200);
            result.setMsg("SUCCESS");
            result.setData(map);
        }
        catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("ERROR");
            result.setData("请求失败");
        }
        return result;
    }

    @Override
    public String getDeptId(String deptName, String orgId) {
        return busComplaintMapper.getDeptId(deptName, orgId);
    }

    @Override
    public String getorgId(String orgName) {
        return busComplaintMapper.getOrgId(orgName);
    }

}
